package com.study.enc;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.script.ScriptUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author zhuzi
 * 2024/11/26 23:0
 */
public class ClsDemo {
    @Test
    public void cls() throws FileNotFoundException {
        File file = ResourceUtils.getFile("classpath:js/cailianshe.js");
        String scrript = FileUtil.readString(file.getPath(), StandardCharsets.UTF_8);
        Object v1 = ScriptUtil.invoke(scrript, "getsign", "app=CailianpressWeb&last_article_time=1732594809&os=web&Subject_Id=1103&sv=8.4.6");

        System.out.println(v1);
    }

    @Test
    public void replace() {
        String articleImg = "https://img.cls.cn/images/20241126/QCOHZEq24o.jpg,https://img.cls.cn/images/20241126/WLP5p00U7z.png";
        String replace = StringUtils.replace(articleImg, ",", "\n");
        System.out.println(replace);


    }


    @Test
    public void param() {
        Map<String, String> params = new TreeMap<>();
        //Subject_Id=1103&app=CailianpressWeb&last_article_time=1732671420&os=web&sv=8.4.6

        params.put("param3", "value3");
        params.put("param1", "value1");
        params.put("param2", "value2");


        String str = "app=CailianpressWeb&last_article_time=1732677199&os=web&Subject_Id=1103&sv=8.4.6";
        List<String> split = StrSplitter.split(str, "&", true, false);

        TreeMap<String, String> treeMap = new TreeMap<>();
        for (String s : split) {
            List<String> split1 = StrSplitter.split(s, '=', true, false);
            if (CollectionUtils.isNotEmpty(split1)) {
                treeMap.put(split1.get(0), split1.get(1));
            }
        }
        System.out.println(treeMap);
        StrBuilder strBuilder = StrBuilder.create();
        treeMap.forEach((k, v) -> {
            strBuilder.append(k).append("=").append(v).append("&");
        });
        String string = strBuilder.delTo(strBuilder.toString().length() - 1).toString();

        System.out.println(string);

        //HttpUtil.toParams(stringStringMap);
    }

    /**
     * 财联社 签名
     *
     * @param treeMap 树状图
     * @return {@link String }
     */
    public static String clsSign(TreeMap<String, String> treeMap) {
        StrBuilder strBuilder = StrBuilder.create();
        treeMap.forEach((k, v) -> {
            strBuilder.append(k).append("=").append(v).append("&");
        });
        String urlParam = strBuilder.delTo(strBuilder.toString().length() - 1).toString();
        // sha-1 和MD5
        String md5Hex = DigestUtil.sha1Hex(urlParam);
        String sign = DigestUtil.md5Hex(md5Hex);
        return urlParam + "&sign=" + sign;
    }

    /**
     * 财联社 签名
     *
     * @param url 树状图
     * @return {@link String }
     */
    public static String clsSign(String url) {
        List<String> split = StrSplitter.split(url, "&", true, false);
        TreeMap<String, String> treeMap = new TreeMap<>();
        for (String s : split) {
            List<String> split1 = StrSplitter.split(s, '=', true, false);
            if (CollectionUtils.isNotEmpty(split1)) {
                treeMap.put(split1.get(0), split1.get(1));
            }
        }
        return clsSign(treeMap);
    }

    @Test
    public void sha1() {
        //按照keyp排序，然后 SHA-1加密签名，然后MD5
        String text = "Subject_Id=1103&app=CailianpressWeb&last_article_time=1732677199&os=web&sv=8.4.6";
        // 第一种：创建Digester对象，执行加密
        Digester md5 = new Digester(DigestAlgorithm.SHA1);
        String digestHex = md5.digestHex(text);
        System.out.println(digestHex);

        // 第二种：执行使用DigestUtil
        String md5Hex = DigestUtil.sha1Hex(text);
        System.out.println(md5Hex);
        String s = DigestUtil.md5Hex(md5Hex);
        System.out.println(s);
        //sign=3c16347fd0baf940b44e192852e57063
        System.out.println(StringUtils.equalsAny(s, "3c16347fd0baf940b44e192852e57063"));

    }

}

